package xin.ryven.workflow.service.impl;

import org.camunda.bpm.engine.repository.ProcessDefinition;
import xin.ryven.workflow.service.ProcessDefinitionService;
import org.camunda.bpm.engine.RepositoryService;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.List;

@Service
public class ProcessDefinitionServiceImpl implements ProcessDefinitionService {
    private final RepositoryService repositoryService;

    public ProcessDefinitionServiceImpl(RepositoryService repositoryService) {
        this.repositoryService = repositoryService;
    }

    @Override
    public List<ProcessDefinition> listProcessDefinitions() {
        return repositoryService.createProcessDefinitionQuery()
                .latestVersion()
                .orderByProcessDefinitionKey()
                .asc()
                .list();
    }

    @Override
    public String getProcessDefinitionXml(String processDefinitionId) {
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
                .processDefinitionId(processDefinitionId)
                .singleResult();
        
        if (processDefinition != null) {
            try (InputStream bpmnStream = repositoryService.getResourceAsStream(
                    processDefinition.getDeploymentId(), 
                    processDefinition.getResourceName())) {
                return new String(bpmnStream.readAllBytes(), StandardCharsets.UTF_8);
            } catch (IOException e) {
                throw new RuntimeException("Error reading BPMN XML", e);
            }
        }
        return null;
    }

    @Override
    public void deployProcessDefinition(String name, String xml) {
        repositoryService.createDeployment()
                .addString(name + ".bpmn", xml)
                .name(name)
                .deploy();
    }

    @Override
    public void deleteProcessDefinition(String processDefinitionId) {
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
                .processDefinitionId(processDefinitionId)
                .singleResult();
        
        if (processDefinition != null) {
            repositoryService.deleteDeployment(processDefinition.getDeploymentId(), true);
        } else {
            throw new RuntimeException("Process definition not found: " + processDefinitionId);
        }
    }

    @Override
    public ProcessDefinition getProcessDefinition(String processDefinitionId) {
        return repositoryService.createProcessDefinitionQuery()
                .processDefinitionId(processDefinitionId)
                .singleResult();
    }

    @Override
    public ProcessDefinition getLatestProcessDefinitionByKey(String key) {
        return repositoryService.createProcessDefinitionQuery()
                .processDefinitionKey(key)
                .latestVersion()
                .singleResult();
    }
} 